const threeWidget = this.pageObj.getApiWidgetByAliasName('三维组件');
const type_selection = this.pageObj.getApiWidgetByAliasName('type_selection');
const type = $(type_selection.__box).find('input[type="radio"]:checked').val();

function breakDataSource(item) {
    if (item.personId) {
        item.title = item.accountName || "测试";
        item.userId = item.account;
        item.treeId = item.account || item.id;
    } else if(item.orgId || item.orgName) {
        item.title = item.orgName;
        item.id = item.orgId;
        item.treeId = item.orgId;
    } else if (item.deptId || item.deptName) {
        item.title = item.deptName;
        item.id = item.deptId;
        item.treeId = item.deptId;
    }

    item.type = '1'; // 叶子节点标识
    if (item.values) {
        item.children = [...item.values].map(breakDataSource);
        item.type = '0'
    }
    if (item.child) {
        item.children = [...item.child].map(breakDataSource);
        item.type = '0'
    }

    return item
}

const artemisWidget = threeWidget?.widgetObj?.artemis;
const { live } = threeWidget?.widgetObj?._option;

const reloadData = () => {
    const { option_data } = this.widgetObj;
    if (option_data) {

        artemis.getOnlineMembers(({ members }) => {
            const dataSrouce = option_data.map(breakDataSource);

            if (type === 'online' && artemisWidget) {
                this.widgetObj.option_data = artemisWidget.filterUsersOnline(dataSrouce);
            } else if (type === 'offline' && artemisWidget) {
                this.widgetObj.option_data = artemisWidget.filterUsersOnline(dataSrouce, true);
            }

            this.widgetObj.render();
            artemisWidget.resizeLive(live);
        });
    }
}

if (artemisWidget) {
    artemisWidget?.startLiveRoom({}, ({userId} = {}) => {
        if (userId) {
            $(`#org_tree #${userId} .opt-item`).each(function () {
                let $item = $(`#org_tree #${userId}`);
                let data = $item.attr('data-nodeobject');
                const rule = $(this).attr('rule');
                if (rule) {
                    try {
                        data = JSON.parse(data);
                        data.classStatus = 'active';
                        $item.attr('data-nodeobject', JSON.stringify(data));
                        $(this).css('display', eval(rule)(data) ? 'block' : 'none')
                    } catch (error) {
                        console.warn(error)
                    }

                }
            })
        }

        reloadData();
    });
}

console.log('直播');

